// javascript for page transform google gadget

function modifyHtml(html, nextPageFullScreen, hrefAppendURL) {               

	html = modPart(html, "<html((?=>)|(?= ))", "i", "<div id='oldHtml'"); //remove html start tag
	html = modPart(html, "</html>", "i", "</div>"); //remove end tag

	html = modPart(html, "<head((?=>)|(?= ))", "i","<div id='oldHead'"); //replace head start tag
	html = modPart(html, "</head>", "i", "</div>"); //replace end tag

	html = modPart(html, "<body((?=>)|(?= ))", "i", "<div id='oldBody'"); //replace body start tag
	html = modPart(html, "</body>", "i", "</div>"); //replace end tag

	if (nextPageFullScreen == 'true') { //then display next page in top frame ie. no longer inside the google gadget
	  html = modPart(html, '<form ', "gi", '<form target="_top" '); //update form tags
	  html = modPart(html, '<a ', "gi", '<a target="_top" '); //update anchor tags
	}
		  
	html = updateLocalLinks(html, "href", "gi", hrefAppendURL);
	html = updateLocalLinks(html, "action", "gi", hrefAppendURL);
	html = updateLocalLinks(html, "src", "gi", hrefAppendURL);

	return html;
};

// modify the html string using regular expressions
function modPart(str, strPattern, strModifiers, strNew) {
	var patt=new RegExp(strPattern, strModifiers);
	var result = str.replace(patt, strNew);
	return result;
};

//add in http address of orgininal local site when it is missing
function updateLocalLinks(html, str, strModifiers, hrefAppendURL) {
	var p2 = ' '+str+'="(?!http)'; //e.g. look for ' href="' not followed by a url starting with http
	var p4 = ' '+str+'="'+hrefAppendURL; //e.g. replace it with ' href="http...' as defined by the configured user preference
	html = modPart(html, p2, strModifiers, p4); //fixes links defined with double quotes
	var p2 = " "+str+"='(?!http)";
	var p4 = " "+str+"='"+hrefAppendURL;
	html = modPart(html, p2, strModifiers, p4); //fixes links defined with single quotes
	return html;
};